package com.xk857.simpleeffect.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.adapter.ListOpinionAdapter;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.entity.Opinion;
import com.xk857.simpleeffect.domain.param.AddOpinionParam;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.toast.XToast;

import java.lang.reflect.Type;
import java.util.List;
import java.util.function.Consumer;

import cn.hutool.core.util.StrUtil;

public class OpinionActivity extends AppCompatActivity {

    private EditText titleEdit;
    private EditText contentEdit;
    private RadioGroup radioGroup;
    private Button addBtn;
    private RecyclerView recyclerView;

    private AddOpinionParam addOpinionParam;
    private LinearLayoutManager layoutManager;
    private ListOpinionAdapter listOpinionAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.white));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_opinion);
        addOpinionParam = new AddOpinionParam();
        addOpinionParam.setType(1);
        initView();

        getOpinionList();
    }

    /**
     * 获取反馈列表
     */
    private void getOpinionList() {
        Type type = new TypeToken<Result<List<Opinion>>>() {}.getType();
        OkHttpUtils.getInstance().get(this, RequestEnum.ADD_OPINION, type, true, (Consumer<List<Opinion>>) opinionList -> {
            runOnUiThread(() -> {
                layoutManager = new LinearLayoutManager(this);
                recyclerView.setLayoutManager(layoutManager);
                listOpinionAdapter = new ListOpinionAdapter(this, opinionList);
                listOpinionAdapter.setItemAnimation(BaseQuickAdapter.AnimationType.SlideInLeft);
                recyclerView.setAdapter(listOpinionAdapter);
            });
        });
    }

    private void initView() {
        TitleBar titleBar = findViewById(R.id.title_bar);
        titleBar.setLeftClickListener(v -> finish());
        titleEdit = findViewById(R.id.title_edit);
        contentEdit = findViewById(R.id.content_edit);
        radioGroup = findViewById(R.id.radioGroup);
        addBtn = findViewById(R.id.add_btn);
        recyclerView = findViewById(R.id.recycler_view);

        radioGroup.setOnCheckedChangeListener((group, checkedId) -> addOpinionParam.setType(checkedId == R.id.bug_btn ? 1 : 2));
        addBtn.setOnClickListener(v -> {
            if (StrUtil.isBlank(titleEdit.getText().toString())) {
                XToast.warning(this,"简要描述不可为空").show();
                return;
            }
            addOpinionParam.setTitle(titleEdit.getText().toString());
            addOpinionParam.setContent(contentEdit.getText().toString());
            Type type = new TypeToken<Result<Opinion>>() {}.getType();
            OkHttpUtils.getInstance().post(this, RequestEnum.ADD_OPINION, type, addOpinionParam, true, (Consumer<Opinion>) opinion -> {
                if (opinion!=null) {
                    runOnUiThread(() -> {
                        titleEdit.setText("");
                        contentEdit.setText("");
                        XToast.success(this,"您的反馈信息已提交").show();
                        listOpinionAdapter.add(0, opinion);
                    });
                }
            });
        });
    }
}